package com.mall.b2c.plugin.search;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.domain.goods.BrandDomain;
import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.b2c.service.BrandService;
import com.mall.common.hibernate.Finder;
import com.mall.common.plugin.AutoRegisterPlugin;
import com.mall.common.util.UploadUtil;
import com.mall.common.util.UrlUtils;


/**
 * 品牌筛选器
 * @author wangzx
 *
 */
public class BrandSearchFilter extends AutoRegisterPlugin implements IGoodsSearchFilter {
	@Resource BrandService brandService;

	@Override
	public void filter(Finder finder, GoodsCategoryDomain cat, String urlFragment) {
		if(StringUtils.isNotBlank(urlFragment) && Pattern.matches("^\\d+$", urlFragment)) {
			Long brandId = Long.valueOf(urlFragment);
			finder.append(" and g.goods.brand.id = :brandId");
			finder.setParam("brandId", brandId);
		}
	}

	@Override
	public List<SearchSelector> createSelectorList(GoodsCategoryDomain cat, String url, 
			String urlFragment, Map<String, String> paramMap, String hql, Finder finder) {
		List<SearchSelector> searchSelectors = new ArrayList<SearchSelector>();
		if(StringUtils.isNotBlank(urlFragment) && Pattern.matches("^\\d+$", urlFragment)){
			Long brandId = Long.valueOf(urlFragment);
			BrandDomain brand = brandService.find(brandId);
			if(brand != null) {
				SearchSelector searchSelector = new SearchSelector();
				searchSelector.setName(brand.getName());
				searchSelector.setIcon(UploadUtil.replacePath(brand.getLogo()));
				searchSelector.setSelected(Boolean.TRUE);
				searchSelector.setUrl(url);
				searchSelectors.add(searchSelector);
				return searchSelectors;
			}
		}
		finder.resetHQL("select g.goods.brand, count(*) from " + hql + " group by g.goods.brand order by count(*) desc");
		finder.setMaxResults(50);
		List<Object[]> resultList = finder.findHQLList();
		if(!CollectionUtils.isEmpty(resultList)) {
			SearchSelector searchSelector = null;
			for(Object[] rs : resultList) {
				BrandDomain brd = (BrandDomain)rs[0];
				searchSelector = new SearchSelector();
				searchSelector.setName(brd.getName());
				searchSelector.setCount((Long)rs[1]);
				searchSelector.setIcon(UploadUtil.replacePath(brd.getLogo()));
				searchSelector.setUrl(UrlUtils.addUrlParam(url, getFilterId(), String.valueOf(brd.getId()), false));
				searchSelector.setSelected(Boolean.FALSE);
				searchSelectors.add(searchSelector);
			}
		} 
		return searchSelectors;
	}

	@Override
	public String getFilterId() {
		return "brand";
	}

	@Override
	public String getSysId() {
		return "brandList";
	}

	@Override
	public String getType() {
		return "goodsSearchFilter";
	}

	@Override
	public String getId() {
		return "brandSearchFilter";
	}

	@Override
	public String getName() {
		return "品牌筛选器";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "wangzx";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}
}
